# -*- coding:utf-8 -*-
# 读取".\Config\testPlan.csv"配置的测试计划
import os
from Util import config
import pandas as pd


class TestSuite(object):

	def __init__(self):
		self._testFiles = []
		self._testSuites = []
		self._testPlanPath = os.path.join(config.configPath, "testPlan.csv")

	# 读取测试计划
	def readTestPlan(self) -> list:
		df = pd.read_csv(self._testPlanPath)
		row = df.shape[0]

		for i in range(row):
			if df.at[i, "是否执行"] == "Y":
				filename = df.at[i, "文件名"]
				self._testFiles.append(filename)
		return self._testFiles

	# 读取测试用例
	def readTestCase(self) -> list:
		from Util import logger
		logger.info("开始加载测试用例")
		for testFile in self.readTestPlan():
			testCasePath = os.path.join(config.configPath, "testSuites", testFile)
			df = pd.read_csv(testCasePath)
			row = df.shape[0]
			for i in range(row):
				if df.at[i, "是否执行"] == "N":
					continue
				"""
				指定某个测试类下的测试方法，用“::”隔开。如：
				命令格式：pytest 文件名.py::测试类::测试方法
				"""
				if df.at[i, "配置方式"] == "1-按方法运行":
					self._testSuites.append(
						f"{os.path.join(df.at[i, '路径'], df.at[i, '文件名'])}::{df.at[i, '类名']}::{df.at[i, '方法名']}")
				elif df.at[i, "配置方式"] == "2-按类名运行":
					self._testSuites.append(f"{os.path.join(df.at[i, '路径'], df.at[i, '文件名'])}::{df.at[i, '类名']}")
				elif df.at[i, "配置方式"] == "3-按文件名运行":
					self._testSuites.append(os.path.join(df.at[i, "路径"], df.at[i, "文件名"]))
		logger.info(f"测试用例为加载完成")
		return self._testSuites


testSuite = TestSuite()

if __name__ == '__main__':
	r = testSuite.readTestCase()
	print(len(r))
	print(r)
